import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.PrintPDF;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;

import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import java.awt.image.BufferedImage;
import java.awt.print.PrinterException;
import java.io.*;
import java.util.Iterator;

/**
 * Created by lyq on 2017/11/30.
 */

public class ReadImageFromPdf {
    public static void main(String[] args) throws Exception {
        ReadImageFromPdf.PDFtoImage("F:\\毕设资料\\毕业论文.pdf");
       /* PrintPDF.main(new String[]{
                "-silentPrint",//静默打印
                "-printerName", "hp Laserjet 1020",//指定打印机名
                "-orientation", "auto|landscape|portrait",//打印方向，三种可选
                "pdfFilePath"//打印PDF文档的路径//
        } );*/

       printFile("E:/text.pdf");

    }

    public static void create() throws IOException{
        PDDocument document = new PDDocument();
        PDPage blankPage = new PDPage();
        document.addPage(blankPage);
        document.save("E:/text.pdf");
        document.close();
    }

    public static void PDFtoImage(String filePath){
        PDDocument pdf = null;
        try{
            pdf = PDDocument.load(new File(filePath));
            PDFRenderer pdfRenderer = new PDFRenderer(pdf);
            PDPageTree pageTree = pdf.getPages();
            int pageCounter = 0;
            for(PDPage page :pageTree){
                BufferedImage bim = pdfRenderer.renderImage(pageCounter,2.5f, ImageType.RGB);
                ImageIOUtil.writeImage(bim,"E:\\test\\"+(pageCounter++)+".jpg",1080);
            }
        } catch (IOException e
                ) {
            e.printStackTrace();
        }
    }


    public static void printFile(String path) throws Exception {
        File pdfFile = new File(path);
        //构建打印请求属性集
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        //设置打印格式，因为未确定文件类型，这里选择AUTOSENSE
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        //查找所有的可用打印服务
        PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
        //定位默认的打印服务
        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
        //显示打印对话框
        //PrintService service = ServiceUI.printDialog(null, 200, 200, printService,   defaultService, flavor, pras);
        if(defaultService!=null){
            DocPrintJob job = defaultService.createPrintJob(); //创建打印作业
            FileInputStream fis = new FileInputStream(pdfFile); //构造待打印的文件流
            DocAttributeSet das = new HashDocAttributeSet();
            Doc doc = new SimpleDoc(fis, flavor, das); //建立打印文件格式
            job.print(doc, pras); //进行文件的打印
        }

    }
}
